package com.wisewin.circle.service;

import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.dao.ActivityDAO;
import com.wisewin.circle.dao.OrgUserDAO;
import com.wisewin.circle.dao.TrendDAO;
import com.wisewin.circle.dao.UserDAO;
import com.wisewin.circle.entity.bo.QueryParamBO;
import com.wisewin.circle.entity.db.OrgUserDO;
import com.wisewin.circle.entity.db.TrendDO;
import com.wisewin.circle.util.DateUtil;
import com.wisewin.circle.util.PageInfo;
import com.wisewin.circle.util.PageObjectUtil;
import com.wisewin.circle.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

@Service
@Transactional
public class OrgUserService {

    @Autowired
    private OrgUserDAO orgUserDAO;
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private ActivityDAO activityDAO;
    @Autowired
    private TrendDAO trendDAO;

    // 查询用户列表
    public ResultDTO queryOrgUserList(QueryParamBO queryParamBO, PageUtil pageUtil){
        Map<String, Object> map = new HashMap<>();
        map.put("userName", queryParamBO.getUserName());
        map.put("loginName", queryParamBO.getLoginName());
        map.put("pageOffset", pageUtil.getPageNo());
        map.put("pageSize", pageUtil.getPageSize());
        PageObjectUtil page = new PageObjectUtil();
        PageInfo pageData = page.savePageObject(orgUserDAO.queryOrgUserCount(map), orgUserDAO.queryOrgUserList(map), pageUtil);
        return ResultDTOBuilder.success(pageData);
    }

    public OrgUserDO queryByLoginName(String loginName, String passcode){
        return orgUserDAO.queryByLoginName(loginName, passcode);
    }

    public OrgUserDO queryOrgUserById(Integer id){
        return orgUserDAO.queryOrgUserById(id);
    }

    // 查询首页统计数据
    public ResultDTO queryHomeData(){
        // 查询用户注册数量
        int userNum = userDAO.queryUserNum();
        // 查询活动数量
        int activityNum = activityDAO.queryActivityNum();
        // 查询动态数量
        int trendNum = trendDAO.queryTrendNum(null);
        // 查询今日发帖数量
        String today = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
        int trendNumToday = trendDAO.queryTrendNum(today);
        // 发帖信息查询
        List<TrendDO> lstTrend = trendDAO.queryTrendLast();
        List<Map<String, Object>> lstTrendUser = trendDAO.queryTrendUser();
        Map<String, Object> result = new HashMap<>();
        result.put("userNum", userNum);
        result.put("activityNum", activityNum);
        result.put("trendNum", trendNum);
        result.put("trendNumToday", trendNumToday);
        result.put("lstTrend", lstTrend);
        result.put("lstTrendUser", lstTrendUser);
        return ResultDTOBuilder.success(result);
    }
}
